
package com.jf.cloud.combo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.combo.vo.ComboSkuVo;
import com.jf.cloud.api.product.feign.SkuFeignClient;
import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.combo.dto.ComboSpuSkuDTO;
import com.jf.cloud.combo.mapper.ComboSpuSkuMapper;
import com.jf.cloud.combo.model.ComboSpuSku;
import com.jf.cloud.combo.service.ComboSpuSkuService;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.vo.ComboSpuSkuVO;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.product.vo.app.SkuAppVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 套装商品sku项
 *
 * @author zz
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboSpuSkuServiceImpl implements ComboSpuSkuService {

    @Autowired
    private ComboSpuSkuMapper comboSpuSkuMapper;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private SkuFeignClient skuFeignClient;
    @Autowired
    private SpuFeignClient spuFeignClient;

    @Override
    public PageVO<ComboSpuSku> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> comboSpuSkuMapper.list());
    }

//    @Override
//    public ComboSpuSku getByComboSpuId(Long comboSpuId) {
//        return comboSpuSkuMapper.getByComboSpuId(comboSpuId);
//    }

    @Override
    public void save(List<ComboSpuSkuVO> comboSpuSkuList, Long comboSpuId) {
        List<ComboSpuSku> comboSpuSkus = new ArrayList<>();
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            ComboSpuSku spuSku = mapperFacade.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkus.add(spuSku);
        }
        comboSpuSkuMapper.save(comboSpuSkus,comboSpuId);
    }

    @Override
    public void update(ComboSpuSku comboSpuSku) {
        comboSpuSkuMapper.update(comboSpuSku);
    }

    @Override
    public void deleteById(Long comboSpuId) {
        comboSpuSkuMapper.delete(comboSpuId);
    }

    @Override
    public void insertBatch(List<ComboSpuSkuVO> skuList, Long comboSpuId) {
        if (CollUtil.isEmpty(skuList) || Objects.isNull(comboSpuId)) {
            return;
        }
        List<ComboSpuSku> comboSpuSkuList = new ArrayList<>();
        for (ComboSpuSkuVO comboSpuSkuVO : skuList) {
            ComboSpuSku comboSpuSku = mapperFacade.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkuList.add(comboSpuSku);
        }
        comboSpuSkuMapper.insertBatch(comboSpuSkuList, comboSpuId);
    }

    @Override
    public void batchUpdate(List<ComboSpuSkuVO> comboSpuSkuList) {
        List<ComboSpuSku> comboSpuSkus = new ArrayList<>();
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            ComboSpuSku comboSpuSku = mapperFacade.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkus.add(comboSpuSku);
        }
        comboSpuSkuMapper.batchUpdate(comboSpuSkus);
    }

    @Override
    public List<ComboSkuVo> comboVoListComboAndSkuList(Set<Long> comboIdList, List<Long> skuIdList) {
        return comboSpuSkuMapper.comboVoListComboAndSkuList(comboIdList,skuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> comboVoListByComboSpuId(Long comboSpuId) {
        return comboSpuSkuMapper.comboVoListByComboSpuId(comboSpuId);
    }

    @Override
    public void deleteBatch(List<Long> comboSpuIdList) {
        comboSpuSkuMapper.deleteBatch(comboSpuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> getByComboSpuId(Long comboSpuId) {
        return comboSpuSkuMapper.getByComboSpuId(comboSpuId);
    }

    @Override
    public List<ComboSpuSkuVO> comboSpuSkuList(List<Long> comboSpuIdList) {
        return comboSpuSkuMapper.comboSpuSkuList(comboSpuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> comboSpuSkuListByComboIdAndSpuId(ComboSpuSkuDTO comboSpuSkuDTO) {
        List<ComboSpuSkuVO> comboSpuSkuList = comboSpuSkuMapper.comboSpuSkuListByComboIdAndSpuId(comboSpuSkuDTO.getComboId(), comboSpuSkuDTO.getSpuId());        List<SkuAppVO> skuAppVOList = skuFeignClient.listBySpuId(comboSpuSkuDTO.getSpuId()).getData();
        Map<Long, SkuAppVO> skuAppVOMap = skuAppVOList.stream().collect(Collectors.toMap(SkuAppVO::getSkuId, e -> e));
        SpuVO spuVO = spuFeignClient.getById(comboSpuSkuDTO.getSpuId()).getData();
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            SkuAppVO skuAppVO = skuAppVOMap.get(comboSpuSkuVO.getSkuId());
            if (Objects.isNull(skuAppVO)){
                continue;
            }
            if (Objects.isNull(skuAppVO.getMainImgUrl())){
                skuAppVO.setMainImgUrl(spuVO.getMainImgUrl());
            }
            comboSpuSkuVO.setPic(skuAppVO.getMainImgUrl());
            if (Objects.isNull(skuAppVO.getSkuName())){
                skuAppVO.setSkuName("");
            }
            comboSpuSkuVO.setSkuName(skuAppVO.getSkuName());
            comboSpuSkuVO.setPriceFee(skuAppVO.getPriceFee());
            comboSpuSkuVO.setProperties(skuAppVO.getProperties());
            comboSpuSkuVO.setStocks(skuAppVO.getStock());
        }
        return comboSpuSkuList;
    }
}
